<template>
           <div class="modal-content">
                <div class="modal-layout1 form" style="width: 500px">
                    <p class="title">导入接口</p>
                    <div class="item">
                        <div class="label col-sm-2">选择项目</div>
                        <div class="col-sm-10 full-text">
                            <select v-on:change="changeProject" class="text" v-model="flag.moveSelectProjectId">
                                <option v-for="item in projects" v-bind:value="item.id">{{item.description}}</option>
                            </select>
                        </div>
                    </div>
                    <div class="item">
                        <div class="label col-sm-2">选择模块</div>
                        <div class="col-sm-10 full-text">
                            <select class="text" v-model="flag.moveSelectModuleId">
                                <option v-for="item in modules" v-bind:value="item.id">{{item.name}}</option>
                            </select>
                        </div>
                    </div>
                    <div class="item" v-for="m in modules" v-if="flag.moveSelectModuleId == m.id">
                        <div class="label col-sm-2">选择分类</div>
                        <div class="col-sm-10 full-text">
                            <select class="text" v-model="flag.moveSelectFolderId">
                                <option v-for="item in m.folders" v-bind:value="item.id">{{item.name}}</option>
                            </select>
                        </div>
                    </div>
                    <div class="item">
                                <div class="label col-sm-2">选择文件</div>
                                <div class="col-sm-10 full-text">
                                     <input v-on:change="change" id="uploadfile" type="file"/>    
                                </div>
                     </div>
                      <div id="expdata" class="item">
                                <div data-id="{{item.name}}" v-for="item in exportData">
                                    <div class="label col-sm-1">
                                        <input type="checkbox" checked data-id="{{item.name}}">
                                    </div>
                                    <div class="label col-sm-6">{{item.name}}</div>
                                    <div class="col-sm-5 full-text">
                                        <span>{{item.description}}</span>
                                    </div>
                                </div>
                                
                     </div>
                    <div class="ta-c actions">
                        <button class="btn btn-default-box middle" tabindex="3"
                                v-on:click="cancel">
                            取消
                        </button>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <button class="btn btn-primary middle" v-on:click="save" tabindex="2">确定</button>
                    </div>
                </div>
            </div>
</template>
<script>
    import '../../src/jquery.min.js';
    import utils from '../../src/utils.js'
    var invites=[];
    // document.getElementById("uploadfile").onchange=function(e){
    //         alert(12);
    // }
    
    export default {
        data: function () {
            return {
                id: 'test',
                flag: { 
                    moveSelectProjectId:null,
                    moveSelectModuleId: null,
                    moveSelectFolderId: null,
                },
                modules: [],
                projects:[],
                exportData:[]
            }
        },
        created:function(){
            var _self=this;
            utils.get('/project/list.json', {}, function (rs1) {
                _self.projects=rs1.data.projects;
            });
            
                
        },
        methods: {
            changeProject:function(){
                var _self=this;
                utils.get('/project/'+this.flag.moveSelectProjectId+'.json', {}, function (rs) {
                    _self.modules=rs.data.modules;
                });
            },
            save:function(){
                var j=0;
                var _self=this;
                var selectItems=[];
                $("#expdata").find("input:checked").each(function(i,item){
                    var name=$(item).attr("data-id");
                    selectItems.push(name);
                });
                var contains=function(name,names){
                    for(var i=0;i<names.length;i++){
                        if(names[i]==name){
                            return true;
                        }
                    }
                    return false;
                }
                var upload=function(item){
                        if(item){
                            if(!contains(item.name,selectItems))
                            {
                                upload(_self.exportData.shift());
                                return;
                            }

                            utils.post('/interface/save.json', item, function (rs) {
                                    $("[data-id='"+item.name+"']").slideUp();
                                    console.log(rs.data+"---保存成功");
                                    upload(_self.exportData.shift());
                            });    
                        }else{
                            _self.exportData=[];
                            $("#expdata").hide();
                           toastr.success('导入成功');
                        }
                }
                upload(_self.exportData.shift());
                  
            },
            cancel:function(){
                _self.exportData=[];
            },
            change:function(event){
                 var _self=this;
                _self.exportData=[];
                var file = event.target.files[0]; 
                console.log(file.name); 
                var prefix=file.name.replace(".html","").replace(".htm","");
                var reader = new FileReader();  
                reader.readAsText(file);  
               
                if(!_self.flag.moveSelectFolderId||!_self.flag.moveSelectModuleId||!_self.flag.moveSelectProjectId){
                        toastr.error("请选择模块");
                        return;
                }
                reader.onload = function(e){ // reader onload start  
                    var html= e.target.result;
                    var strHelper=new window.js.lang.String();
                    var $html=$(html);
                    var $ele=$html.find("[name='method.detail']");
                    var blockList=$ele.siblings(".blockList,.blockListLast");

                    $.each(blockList,function(i,item){
                        var $content=$(item).find("li.blockList");
                        var $pre=$content.find("pre");
                        var pre=strHelper.decodeHtml($content.find("pre").html()).replace("\r\n","").replace("@PathVariable","");
                        var responseBody=/@ResponseBody/g.exec(pre);
                        var paramsStr=/public [\w.<>,]+ [\w\d_]+\(([\s\S]+)/g.exec(pre);
                        var paramTypeObj=[];
                       
                        if(paramsStr[1]){
                            var seconProcess=paramsStr[1].match(/([\w.<>]+ [\w.<>]+)/g);
                            if(seconProcess){
                                seconProcess.forEach(function(item,i){
                                   paramTypeObj[item.split(" ")[1]]=item.split(" ")[0];
                                });
                            }
                            
                        }
                        console.log(paramTypeObj);
                        if(responseBody&&responseBody.length>0){
                            //包含ResponseBody才是对外接口@RequestMapping\(value\=\"([\/\w+]+?)
                            var methodObj={
                                folderId:_self.flag.moveSelectFolderId,
                                contentType:"JSON",
                                moduleId:_self.flag.moveSelectModuleId,
                                projectId:_self.flag.moveSelectProjectId,
                                dataType:"X-WWW-FORM-URLENCODED",
                                protocol:"HTTP",
                                status:"ENABLE",
                                sort:0
                            };
                            var urlResult=/value="([\w/{}]+?)"/g.exec(pre);
                            methodObj.url="$baseurl$"+urlResult[1];
                            
                            methodObj.requestHeaders=[];
                            var httpMethodResult=/method=(GET|POST)/g.exec(pre);
                            methodObj.requestMethod="GET";
                            if(httpMethodResult&&httpMethodResult.length>1){
                                methodObj.requestMethod=httpMethodResult[1];
                            }
                            var returnType="void";
                            methodObj.name="";
                            var methodInfoResult=/public ([\w.\d><,]+) ([\w\d]+)/g.exec(pre);
                            if(methodInfoResult&&methodInfoResult.length>2){
                                returnType=methodInfoResult[1];
                                methodObj.name=prefix+"."+methodInfoResult[2];
                            }
                            methodObj.responseArgs=[];
                            methodObj.responseArgs.push({
                                "require": "true",
                                "children": [],
                                "type": "object",
                                "name":returnType,
                                "description": ""
                            }) ;
                            var prenext=$pre.next()[0];
                            methodObj.description ="该接口未注释";
                            if(prenext&&prenext.tagName=="DIV"){
                                //有方法注释
                                methodObj.description=$(prenext).html();
                            }
                            //处理参数
                            var dl=$content.find("dl");
                            var dts=$content.find("dl").children();

                            var index=0;
                            var currentType="";
                            methodObj.requestArgs=[];

                            dts.each(function(i,item){
                                var $item=$(item);
                                if($item[0].tagName=="DT"){
                                    currentType=$item.find("span").attr("class");
                                }
                                if($item[0].tagName=="DD"){
                                    switch(currentType){
                                        case "paramLabel":
                                            var paramName=$item.find("code:first").html();
                                            $item.find("code:first").remove();
                                            methodObj.requestArgs.push({
                                                require:"true",
                                                children:[],
                                                type:paramTypeObj[paramName],
                                                name:paramName,
                                                description:$item.html().replace(" - ","")
                                            });
                                            break;
                                        case "returnLabel":
                                            var example=$item.find("code:first").html();
                                            $item.find("code:first").remove();
                                            methodObj.example=example;
                                            methodObj.responseArgs[0].description=$item.html();
                                            break;
                                    }
                                }

                            });
                            methodObj.requestArgs=JSON.stringify(methodObj.requestArgs);
                            methodObj.responseArgs=JSON.stringify(methodObj.responseArgs);
                            methodObj.requestHeaders=JSON.stringify(methodObj.requestHeaders);
                            _self.exportData.push(methodObj);
                            return;
                        }
                        
                    })
                    //保存api文档
                } // reader onload end  
            }
        }
    }
</script>